package gui.dialog;

import gui.TuristAssistant;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.example.www.communication.MailService.EMail;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
/**
 * Dialogo que realiza la funcionalidad de enviar un e-mail utilizando el
 * web-service asociado a travez del cliente de mails. 
 */
public class CommSendEMailDialog extends org.eclipse.swt.widgets.Dialog {
	
	/**
	 * El destinatario del e-mail.
	 */
	private String	eMailRecipient;
	
	private Label	swtlblSubject;
	private Text	swttxtFrom;
	private Text	swttxtTo;
	private Button	swtbtnSend;
	private Text	swttxtaBody;
	private Text	swttxtSubject;
	private Label	swtlblTo;
	private Label	swtlblFrom;
	
	private Shell	dialogShell;
	
	/**
	 * Auto-generated main method to display this org.eclipse.swt.widgets.Dialog
	 * inside a new Shell.
	 */
	public static void main(String[] args) {
		try {
			Display display = Display.getDefault();
			Shell shell = new Shell(display);
			CommSendEMailDialog inst = new CommSendEMailDialog(shell, SWT.NULL);
			inst.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Constructor basado en el constructor super.
	 */
	public CommSendEMailDialog(Shell parent, int style) {
		super(parent, style);
	}
	
	/**
	 * Abre el dialogo n modo modal y lo muestra por pantalla.
	 * <BR/>
	 * Antes de invocar este metodo se deve setear el e-mail mediante
	 * {@link #setEMailRecipient(String)}.
	 */
	public void open() {
		try {
			Shell parent = getParent();
			dialogShell = new Shell(parent, SWT.DIALOG_TRIM
					| SWT.APPLICATION_MODAL);
			
			dialogShell.setLayout(new FormLayout());
			dialogShell.setText("Send e-Mail");
			dialogShell
					.setImage(new Image(Display.getCurrent(),
							"resource/tango-icon-theme-0.8.90/16x16/actions/mail-message-new.png"));
			{
				swtbtnSend = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
				FormData swtbtnSendLData = new FormData();
				swtbtnSendLData.width = 429;
				swtbtnSendLData.height = 30;
				swtbtnSendLData.left = new FormAttachment(27, 1000, 0);
				swtbtnSendLData.top = new FormAttachment(863, 1000, 0);
				swtbtnSendLData.right = new FormAttachment(974, 1000, 0);
				swtbtnSendLData.bottom = new FormAttachment(1000, 1000, -12);
				swtbtnSend.setLayoutData(swtbtnSendLData);
				swtbtnSend.setText("Send e-Mail");
				swtbtnSend.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseUp(MouseEvent evt) {
						swtbtnSendMouseUp(evt);
					}
				});
				swtbtnSend
				.setImage(new Image(Display.getCurrent(),
						"resource/tango-icon-theme-0.8.90/32x32/actions/mail-reply-sender.png"));

			}
			{
				swttxtaBody = new Text(dialogShell, SWT.MULTI | SWT.WRAP
						| SWT.V_SCROLL | SWT.BORDER);
				FormData swttxtaBodyLData = new FormData();
				swttxtaBodyLData.width = 414;
				swttxtaBodyLData.height = 127;
				swttxtaBodyLData.left = new FormAttachment(29, 1000, 0);
				swttxtaBodyLData.top = new FormAttachment(385, 1000, 0);
				swttxtaBodyLData.bottom = new FormAttachment(821, 1000, 0);
				swttxtaBodyLData.right = new FormAttachment(975, 1000, 0);
				swttxtaBody.setLayoutData(swttxtaBodyLData);
				swttxtaBody.setText("Write here your e-mail");
			}
			{
				swttxtSubject = new Text(dialogShell, SWT.BORDER);
				FormData swttxtSubjectLData = new FormData();
				swttxtSubjectLData.width = 372;
				swttxtSubjectLData.height = 13;
				swttxtSubjectLData.left = new FormAttachment(142, 1000, 0);
				swttxtSubjectLData.top = new FormAttachment(271, 1000, 0);
				swttxtSubjectLData.bottom = new FormAttachment(328, 1000, 0);
				swttxtSubjectLData.right = new FormAttachment(974, 1000, 0);
				swttxtSubject.setLayoutData(swttxtSubjectLData);
				swttxtSubject.setText("Write here the subject of this e-mail");
			}
			{
				swttxtTo = new Text(dialogShell, SWT.BORDER);
				FormData swttxtToLData = new FormData();
				swttxtToLData.width = 372;
				swttxtToLData.height = 13;
				swttxtToLData.left = new FormAttachment(142, 1000, 0);
				swttxtToLData.top = new FormAttachment(163, 1000, 0);
				swttxtToLData.bottom = new FormAttachment(219, 1000, 0);
				swttxtToLData.right = new FormAttachment(974, 1000, 0);
				swttxtTo.setLayoutData(swttxtToLData);
				if (this.eMailRecipient == null)
					swttxtTo.setText("Write here the recipient of this e-mail");
				else
					swttxtTo.setText(this.eMailRecipient);
			}
			{
				swttxtFrom = new Text(dialogShell, SWT.BORDER);
				FormData swttxtFromLData = new FormData();
				swttxtFromLData.width = 372;
				swttxtFromLData.height = 13;
				swttxtFromLData.left = new FormAttachment(142, 1000, 0);
				swttxtFromLData.top = new FormAttachment(54, 1000, 0);
				swttxtFromLData.bottom = new FormAttachment(110, 1000, 0);
				swttxtFromLData.right = new FormAttachment(974, 1000, 0);
				swttxtFrom.setLayoutData(swttxtFromLData);
				swttxtFrom.setText(TuristAssistant.appContextProperties
						.getProperty(TuristAssistant.USERID_PROPERTY));
				swttxtFrom.setEditable(false);
				swttxtFrom.setBounds(63, 12, 372, 13);
			}
			{
				swtlblSubject = new Label(dialogShell, SWT.NONE);
				FormData swtlblSubjectLData = new FormData();
				swtlblSubjectLData.width = 39;
				swtlblSubjectLData.height = 13;
				swtlblSubjectLData.left = new FormAttachment(27, 1000, 0);
				swtlblSubjectLData.top = new FormAttachment(271, 1000, 0);
				swtlblSubjectLData.bottom = new FormAttachment(328, 1000, 0);
				swtlblSubjectLData.right = new FormAttachment(115, 1000, 0);
				swtlblSubject.setLayoutData(swtlblSubjectLData);
				swtlblSubject.setText("Subject:");
			}
			{
				swtlblTo = new Label(dialogShell, SWT.NONE);
				FormData swtlblToLData = new FormData();
				swtlblToLData.width = 16;
				swtlblToLData.height = 13;
				swtlblToLData.left = new FormAttachment(27, 1000, 0);
				swtlblToLData.top = new FormAttachment(163, 1000, 0);
				swtlblToLData.bottom = new FormAttachment(219, 1000, 0);
				swtlblToLData.right = new FormAttachment(63, 1000, 0);
				swtlblTo.setLayoutData(swtlblToLData);
				swtlblTo.setText("To:");
			}
			{
				swtlblFrom = new Label(dialogShell, SWT.NONE);
				FormData swtlblFromLData = new FormData();
				swtlblFromLData.width = 26;
				swtlblFromLData.height = 13;
				swtlblFromLData.left = new FormAttachment(27, 1000, 0);
				swtlblFromLData.top = new FormAttachment(54, 1000, 0);
				swtlblFromLData.bottom = new FormAttachment(110, 1000, 0);
				swtlblFromLData.right = new FormAttachment(86, 1000, 0);
				swtlblFrom.setLayoutData(swtlblFromLData);
				swtlblFrom.setText("From:");
			}
			dialogShell.layout();
			dialogShell.pack();
			dialogShell.setLocation(getParent().toDisplay(100, 100));
			dialogShell.open();
			Display display = dialogShell.getDisplay();
			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void swtbtnSendMouseUp(MouseEvent evt) {
		try {
			EMail email = new EMail(this.swttxtFrom.getText(), this.swttxtTo
					.getText(), this.swttxtSubject.getText(), this.swttxtaBody
					.getText());
			
			TuristAssistant.mailClient.sendEMail(email);
			this.dialogShell.dispose();
		} catch (Exception e) {
			MessageBox dialog = new MessageBox(this.getParent().getShell(),this.getStyle()|SWT.ERROR|SWT.OK);
			dialog.setMessage(e.getMessage());
			dialog.setText("Send e-Mail Error");
			dialog.open();
			this.dialogShell.dispose();
		}
	}
	
	/**
	 * @param mailRecipient
	 *        the eMailRecipient to set
	 */
	public synchronized final void setEMailRecipient(String mailRecipient) {
		eMailRecipient = mailRecipient;
	}
	
}
